package cl.pds.pruebacliente;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import com.google.gson.JsonObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Pestana1 extends Activity {
	
	private String URL = "http://172.22.204.151:8080/PDSPrueba1WS/services/Prueba1PdsORMWS";
	private String NAMESPACE = "http://ws.pds.prueba1";
	
	EditText nombreEditText;
	EditText direccionEditText;
	EditText RUTEditText;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pestana1);
		nombreEditText = (EditText) findViewById(R.id.edTxtNombre);
		direccionEditText = (EditText) findViewById(R.id.edTxtDireccion);
		RUTEditText = (EditText) findViewById(R.id.edTxtRUT);
	}
	

	public void ingresar(View v){
		JsonObject jsonObjectPaciente = new JsonObject();
		
		jsonObjectPaciente.addProperty("RUT", RUTEditText.getText().toString());
		jsonObjectPaciente.addProperty("nombre", nombreEditText.getText().toString());
		jsonObjectPaciente.addProperty("direccion", direccionEditText.getText().toString());
		new AddPacienteBackground().execute(jsonObjectPaciente.toString());
	}

	class AddPacienteBackground extends AsyncTask<String, Void, String>{
		
		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
    		progressDialog = ProgressDialog.show(
    				Pestana1.this,
    				"Ingreso de Pacientes", 
    				"Espere mientras se agrega el Paciente...",
    				true);
			super.onPreExecute();
		}
		
		@Override
		protected String doInBackground(String... params) {

			String resultado = new String();
			String METHOD_NAME = "addPaciente";
			String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("jsonPaciente", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			//Valido que tenga la informacion necesaria
			if("1".equals(result)){
				Toast.makeText(Pestana1.this, "Ingresado correctamente", Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(Pestana1.this, "Error al ingresar la informacion enviada", Toast.LENGTH_LONG).show();
			}
		}
	}

}
